/**
 * 加
 * @param {number} arg1
 * @param {number} arg2
 */
export function add(arg1, arg2) {
  const s1 = arg1.toString()
  const s2 = arg2.toString()
  const arg1Arr = s1.split('.')
  const arg2Arr = s2.split('.')
  const d1 = arg1Arr.length === 2 ? arg1Arr[1] : ''
  const d2 = arg2Arr.length === 2 ? arg2Arr[1] : ''
  const maxLen = Math.max(d1.length, d2.length)
  const m = Math.pow(10, maxLen)
  return Number(((s1 * m + s2 * m) / m).toFixed(maxLen))
}

/**
 * 减
 * @param {number} arg1
 * @param {number} arg2
 */
 export function sub(arg1, arg2) {
  return add(arg1, -arg2)
}

/**
 * 乘，原生计算在处理因为 js 本身会出现偏差
 * 如：14 * 0.1 = 1.4000000000000001 这是不合理的
 * @param {number} arg1
 * @param {number} arg2
 * @example
 *  mul(14, .1) => 1.4
 *  mul(NaN, NaN) => NaN
 *  mul(Infinity, NaN) => NaN
 *  mul(Infinity, Infinity) => Infinity
 *  mul(Infinity, -2) => -Infinity
 *  mul(Infinity, 2) => Infinity
 *  mul(Infinity, 0) => NaN
 */
 export function mul(arg1, arg2) {
  let m = 0
  const s1 = arg1.toString()
  const s2 = arg2.toString()
  try {
    m += s1.split('.')[1].length
  } catch (e) {
    //
  }
  try {
    m += s2.split('.')[1].length
  } catch (e) {
    //
  }
  return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m)
}

/**
 * 除
 * @param {number} arg1
 * @param {number} arg2
 * @example
 *  div(0.4, 0.02) => 20
 *  div(4, 2) => 2
 *  div('4', '2') => 2
 *  div(4, 0) => Infinity
 *  div(0, 0) => NaN
 *  div(0, 'l') => NaN
 *  div('u', 'i') => NaN
 *  div(Infinity, Infinity) => NaN
 *  div(NaN, NaN) => NaN
 */
 export function div(arg1, arg2) {
  let t1 = 0
  let t2 = 0
  try {
    t1 = arg1.toString().split('.')[1].length
  } catch (e) {
    //
  }
  try {
    t2 = arg2.toString().split('.')[1].length
  } catch (e) {
    //
  }
  const r1 = Number(arg1.toString().replace('.', ''))
  const r2 = Number(arg2.toString().replace('.', ''))
  return r1 / r2 * Math.pow(10, t2 - t1)
}
